#include "adcproc.h"
#include "pc.h"
#include "usart.h"
#include "string.h"

// adc数据包，adc采集数据时直接放到数据包里，采集完一帧后再添加包头包尾
uint8_t ADCDataPack[ADC_PACK_SIZE] = {0};
uint16_t ADCDataLen = 8000;
uint8_t AdcEnable = 0x00;
// uint16_t adcDataSize = 2000;


uint8_t _GenDataCheckCode(uint8_t *cmd, int start, int end)
{
    uint8_t ans = 0;
    for (int i = start; i <= end; i++)
        ans += cmd[i];
    return ans;
}

// dataSize是ADC采集点数，不是ADC_PACK长度
void sendAdcVal(uint16_t dataSize)
{
    if (dataSize > ADC_PACK_SIZE)
        return;
    if (AdcEnable == 1)
    {
        ADCDataPack[0] = 0xE0;
        ADCDataPack[1] = (dataSize + 4) >>8;
        ADCDataPack[2] = (dataSize + 4) &0xff;
        ADCDataPack[3] = 0xC0;
        // memcpy(ADCDataPack + 4, adc_buff, dataSize);

        ADCDataPack[dataSize + 4] = _GenDataCheckCode(ADCDataPack, 1, dataSize + 3);

        HAL_UART_Transmit_IT(&huart1, ADCDataPack, dataSize + 5);   // 使用中断方式发送，不会阻塞ADC采集数据
    }
}
